OPTIONAL Sprite-animation.f

Requires Structs
Requires FixedPoint
Requires SORA/Spritesets

Only Forth Definitions

Struct
   cell field ->counter
   cell field ->frame#
   cell field ->sprite#
   cell field ->frame
   cell field ->spriteset
   cell field ->speed
End-struct /Sprite-controller

:macro @frame  ->frame @ ;

: frame-addr  ( sprctl -- frame )
   dup ->frame# @ swap ->spriteset @ frame[] ;

: animstep  ( sprctl -- )
   locals| sc |
   sc ->speed @ if
      sc ->counter @  sc ->speed @  -  dup sc ->counter !  dup  0 <= if
         sc ->frame# @  sc ->spriteset @  next-frame  dup -1 > if sc ->frame# !
            sc frame-addr  sc ->frame !
            drop
            sc ->frame @  @delay  s>.  sc ->counter +!
         else  drop then
      else
         drop
      then
   then ;

: !frame   ( frame# sprctl -- )
   swap over ->frame# !
   dup frame-addr  swap ->frame ! ;

: !sprite  ( sprite# sprctl -- )
   >r
   r@ ->sprite# !
   r@ ->sprite# @  r@ ->spriteset @  first-frame  r@ ->frame# !
   r@ frame-addr  r@ ->frame !
   r@ ->frame @  @delay  s>.  r> ->counter ! ;
